<?php


namespace app\api\controller;


use app\BaseController;
use app\middleware\Auth;
use think\middleware\AllowCrossDomain;

class Member extends BaseController
{
    protected $middleware = [AllowCrossDomain::class, Auth::class];
    protected $moduleid = 2;

    public function index()
    {

    }

    public function add()
    {
        $post = $this->request->post();

        $username = $post['username'];
        if (!$username) {
            return $this->msg(0, '账号不能为空');
        }
        $password = $post['password'];
        if (!$password) {
            return $this->msg(0, '密码不能为空');
        }
        $e = \think\facade\Db::name('member')->where(['username' => $username])->field('userid')->find();
        if ($e) {
            return $this->msg(0, '账号已经存在');
        }

        $post['password'] = md5($post['password']);
        $insertid = \think\facade\Db::name('member')->insertGetId($post);
        return $this->msg($insertid);
    }

    public function mod()
    {
        $post = $this->request->post();
        $userid = intval($post['userid']);
        if (!$userid) {
            return $this->msg(0);
        }
        if (!$post['password']) {
            unset($post['password']);
        } else {
            $post['password'] = md5($post['password']);
        }
        $r = \think\facade\Db::name('member')->where(['userid' => $userid])->update($post);
        return $this->msg($r === false ? false : true);
    }

    public function preview()
    {
        $userid = (int)$this->request->post('userid', 0);
        $formated = (int)$this->request->post('formated', 0);
        $data = \think\facade\Db::name('member')->where(['userid' => $userid])->find();
        if (!$data) {
            return $this->msg(0, '数据不存在');
        }
        if ($formated) {
            $data['groupid'] = myswitch($data['groupid'], $this->core_enums['member_group']);
        }
        return $this->msg(1, '', '', $data);
    }

    public function lst()
    {
        $page = (int)$this->request->post('page', 1);
        $userid = (int)$this->request->post('userid', 0);
        $username = trim($this->request->post('username', ''));
        $groupid = (int)$this->request->post('groupid', 0);
        $truename = trim($this->request->post('truename', ''));
        $where = [];
        if ($userid) {
            $where[] = ['userid', '=', $userid];
        }
        if ($username) {
            $where[] = ['username', '=', $username];
        }
        if ($groupid) {
            $where[] = ['groupid', '=', $groupid];
        }
        if ($truename) {
            $where[] = ['truename', 'like', "%{$truename}%"];
        }

        $rows = \think\facade\Db::name('member')->where($where)->order('userid desc')->page($page, 20)->select()->toArray();
        $total = \think\facade\Db::name('member')->where($where)->count('*');
        foreach ($rows as $k => $v) {
            $rows[$k]['groupid'] = myswitch($v['groupid'], $this->core_enums['member_group']);
        }
        return $this->msg(1, '', '', [
            'rows' => $rows,
            'total' => $total,
        ]);
    }

    public function del()
    {
        $userid = (int)$this->request->post('userid', 0);
        $r = \think\facade\Db::name('member')->where(['userid' => $userid])->update([
            'groupid' => 3,
        ]);
        return $this->msg($r);
    }
}